package pl.ptemich.nov2k13.engine;

import pl.ptemich.nov2k13.engine.HexBoard.HexType;


/**
 * Vertexes positions
 * 
 * 
 *       1
 *     /   \
 *    0     2
 *    |     |
 *    5     3
 *     \   /
 *       4  
 *             
 * 
 * @author Brutusek
 *
 */
public class PointyOrientationHex extends AbstractHex {

  public PointyOrientationHex(int side, float posX, float posY, int hexX, int hexY, float h, float r) {
    init(side, posX, posY, hexX, hexY, h, r, HexType.POINTY_ORIENTATION);

    vertexes = new SimplePoint[6];
    vertexes[0] = new SimplePoint(posX - r, posY - (side / 2));
    vertexes[1] = new SimplePoint(posX, posY - (side / 2) - h);
    vertexes[2] = new SimplePoint(posX + r, posY - (side / 2));
    vertexes[3] = new SimplePoint(posX + r, posY + (side / 2));
    vertexes[4] = new SimplePoint(posX, posY + (side / 2) + h);
    vertexes[5] = new SimplePoint(posX - r, posY + (side / 2));

    // a = (y1 - y2) / (x1 + x2)
    a1Factor = (vertexes[0].y - vertexes[1].y) / (vertexes[0].x - vertexes[1].x);
    a2Factor = (vertexes[1].y - vertexes[2].y) / (vertexes[1].x - vertexes[2].x);

    // b = y2 - a * x2
    b1tFactor = vertexes[1].y - a1Factor * vertexes[1].x;
    b2tFactor = vertexes[2].y - a2Factor * vertexes[2].x;
    b1bFactor = vertexes[4].y - a1Factor * vertexes[4].x;    
    b2bFactor = vertexes[5].y - a2Factor * vertexes[5].x;
  }


  @Override
  public boolean checkSelection(int mouseX, int mouseY) {
    if (mouseX > (center.x - r) && mouseX < (center.x + r)) {
      // reversed screen coordinates !!! Y rises to the bottom
      if (mouseY - 1 > a1Factor * mouseX + b1tFactor) { // top left
        if (mouseY + 1 < a1Factor * mouseX + b1bFactor) { // bottom right
          if (mouseY - 1 > a2Factor * mouseX + b2tFactor) { // top right
            if (mouseY + 1 < a2Factor * mouseX + b2bFactor) { // bottom left
              return true;
            }
          }
        }
      }
    }
    return false;
  }

}
